<?php

class User_Widget_Browse_Profile_Member extends Nuo_Widget
{

	function execute()
	{
		$mainResource = Nuo::getVar('mainResource');

		$user = Nuo::getUser();

		$userTable = Nuo::getTable('user');
		
		$memberTable = Nuo::getTable('membership/member');

		$t1 = Nuo::getT('user');

		$select1 = $memberTable -> select()
		 -> from($memberTable -> getName(), 'user_id')
		 -> where('resource_id=?', $mainResource -> getItemId());

		$select = $userTable -> select() -> where('user_id IN ?', $select1);

		$filter = $this -> getParam('filter');

		$userId = Nuo::getUserId();

		/**
		 * filter by mutual friend
		 */
		if ($userId && $filter == 'mutual')
		{
			$select2 = $memberTable -> select()
			-> from($memberTable -> getName(), 'user_id') 
			-> where('resource_id=?', $user -> getItemId());

			$select -> where('user_id IN ?', $select2);
		}

		if ($userId && $filter == 'mayknow')
		{

		}

		if ($filter == 'list' && $listId = $request -> getParam('list_id'))
		{

			$listDataTable = Nuo::getTable('membership/data');

			$select3 = $listTable -> select() -> from($listDataTable -> getName(), 'member_id') -> where('list_id=?', $listId);
			$select -> where('member_id IN ?', $select3);

		}

		$itemApi = Nuo::getApi('membership');

		$paging = Nuo_Paging::factory($select);

		return $this -> render(array(
			'paging' => $paging,
			'resource' => $mainResource,
			'mutual' => $itemApi -> getMutualMemberCount($mainResource, $user),
			'total' => $itemApi -> getMemberCount($mainResource),
			'count' => $paging -> getItemCount(),
		));
	}

}
